package cheme.ProcessModel.Equations;

import java.io.Serializable;

import org.eclipse.draw2d.geometry.Point;

import cheme.ProcessModel.AbstractParent;
import cheme.ProcessModel.ConsoleModel;
import cheme.ProcessModel.IChild;
import cheme.ProcessModel.IModelElement;
import cheme.ProcessModel.ITranslatable;
import cheme.ProcessModel.ModelEnv;
import cheme.ProcessModel.ProcessModelConstants;
import cheme.ProcessModel.SolvabilityStatus;
import cheme.ProcessModel.Visitors.IModelVisitor;

/*
 * Within the chemical proces model, this type present a (nested as a child--EquationList) list of equations that relate
 * unknown tag component quantities to known quantities
 */

public class Equations extends AbstractParent<IChild> implements ITranslatable, Serializable
{
	/*
	 * Serialization version identifier. (Set to default for now)
	 */
	
	private static final long serialVersionUID = 1L;

	/*
	 * Default indices of this model element's primary children.
	 */
	
	public static final int PALETTE_INDEX = 0;
	public static final int LIST_INDEX = 1;
	public static final int STATUS_INDEX = 2;	
	/*
	 * Stores the current placement of the Equations figure within the model's
	 * canvas.
	 */
	
	protected Point location = null;

	public Equations(IModelElement parent)
	{
		super(parent);

		super.add(new EquationPalette(this), PALETTE_INDEX);
		add((EquationList)ModelEnv.getInstanceFor(this).createModelElement(
				ProcessModelConstants.EQUATION_LIST, this), LIST_INDEX);
		
		add((SolvabilityStatus)ModelEnv.getInstanceFor(this).createModelElement(
				ProcessModelConstants.SOLVABILITY_STATUS, this), STATUS_INDEX); // PA change for Solvability Status

		
	}
	

	// private void writeObject(java.io.ObjectOutputStream out) throws
	// IOException
	// {
	// }

	// private void readObject(java.io.ObjectInputStream in) throws IOException,
	// ClassNotFoundException
	// {
	// }
			
	/**
	 * The primaryDescriptor describes the stream "overall flow"
	 * 
	 * @return the primaryDescriptor
	 */
	public EquationList getEquationList()
	{
		if(super.getChildren().size() > LIST_INDEX)
		{
			return (EquationList)get(LIST_INDEX);
		}
		
		return null;
	}
	
	public void setLocation(Point location)
	{
		if(!this.location.equals(location))
		{
			Point oldBounds = this.location;
			
			this.location = location;
	
			firePropertyChange(P_CONSTRAINT, oldBounds, location);
		}
	}
	
	public Point getLocation()
	{
		if(null == location)
		{
			location = new Point(470,10);//85
		}
		
		return location;
	}
	
	/*
	 *--------------------------------------------------------------------------
	 * Equation specific methods for adding/removing children--since this type
	 * is not a direct parent of equations and simply provides convenience
	 * methods for adding equations to the EquationList.
	 */
	
	public void add(Equation child)
	{
		getEquationList().add(child);
	}
	
	public void add(Equation child, int index)
	{
		getEquationList().add(child, index);
	}
	
	public void remove(Equation child)
	{
		getEquationList().remove(child);
	}
	
	/*
	 *--------------------------------------------------------------------------
	 * Seperates individual children text representations.
	 */
	
	@Override
	protected String getToStringSeparator()
	{
		return "\n************************************\n";
	}
	
	public Object accept(IModelVisitor ruleChecker)
	{
		return ruleChecker.visit(this);
	}
}
